package com.bingxu.flink.chapter02.transformation;

import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @author :BingXu
 * @description :TODO
 * @date :2021/8/9 11:34
 * @modifier :
 */

public class Trans_Reduce {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(3);
        DataStreamSource<Integer> intDS = env.fromElements(1, 2, 3, 5, 6, 2, 21, 8, 91, 45);
        DataStreamSource<Integer> int1DS = env.fromElements(22, 32, 34, 56, 21, 8, 91, 45);
        DataStreamSource<Integer> int2DS = env.fromElements(99, 100, 145);
        DataStreamSource<String> strDS = env.fromElements("hello", "world", "hello", "flink", "spark");


        intDS
                .keyBy((KeySelector<Integer, String>) integer -> integer % 2 == 0 ? "even" : "odd number")
                .reduce((ReduceFunction<Integer>) (integer, t1) -> integer + t1)
                .print();

        env.execute();

    }
}
